package com.adaxi.adc.business.passenger.controller;


import com.adaxi.adc.business.passenger.pojo.AdcPassenger;
import com.adaxi.adc.business.passenger.service.AdcPassengerService;
import com.adaxi.adc.business.passenger.utils.JwtUtil;
import com.adaxi.adc.business.passenger.vo.AdcPassengerVo;
import com.adaxi.adc.common.common.CommonResult;
import com.adaxi.adc.common.utils.UuidUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {

    @Autowired
    AdcPassengerService iadcPassengerService;

    @Autowired
    JwtUtil jwtUtil;

  @PostMapping("/login")
    public CommonResult login(@RequestBody AdcPassengerVo adcPassengerVo){
        QueryWrapper<AdcPassenger> wrapper  =new QueryWrapper(); //查询包裹体
        wrapper.eq("username", adcPassengerVo.getUsername()).eq("password", adcPassengerVo.getPassword());
        try {
            AdcPassenger adcPassenger = iadcPassengerService.getOne(wrapper,true);
            //生成TOKEN
            String token =jwtUtil.generateToken(adcPassenger.getUsername());
            String a= String.valueOf(adcPassenger);
            HashMap<String,String> user = new HashMap<>();
            user.put("token",token);
            user.put("user",a);
            return CommonResult.SUCCESS("登录成功",user);



        }catch (Exception e){
            return CommonResult.FAILURE("用户名或者密码错误");

        }

    }

    @GetMapping("/test")
    public CommonResult test(){
      return CommonResult.SUCCESS("测试成功");
    }

    @PostMapping("/addPassenger")
    public CommonResult add(@RequestBody AdcPassenger adcPassenger){
        adcPassenger.setId(UuidUtil.get32UUID());
        iadcPassengerService.save(adcPassenger);
        return CommonResult.SUCCESS("添加成功");

    }
}
